<?php
/**
 * Created by PhpStorm.
 * User: leixiaotian
 * Date: 2020/10/10
 * Time: 15:58
 */
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ChatLog extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'chat_log';

    /**
     * 聊天记录列表
     * @param Request $request
     * @return mixed
     */
    public function getChatLogList(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
//        $sort = $request->get('sort', 'asc');
        $uuid1 = $request->get('uuid1', null);
        $uuid2 = $request->get('uuid2', null);
        $begin = $request->get('begin','') ? $request->get('begin', ''). ' 00:00:00' : '';
        $end = $request->get('end','') ? $request->get('end', ''). ' 23:59:59' : '';

        $query = new ChatLog();

        if($begin && !$end) $query = $query->where('dateTime','>=', $begin);
        if(!$begin && $end) $query = $query->where('dateTime','<=', $end);
        if($begin && $end) $query = $query->whereBetween('dateTime',[$begin,$end]);

        $userInfo1 = DB::connection('api')->table('user')->select('user_id')->where('uuid', '=', $uuid1)->first();
        $userInfo2 = DB::connection('api')->table('user')->select('user_id')->where('uuid', '=', $uuid2)->first();

        if($userInfo1 && $userInfo2 == null) {
            $query = $query->where($this->table.'.user_id', '=', $userInfo1->user_id);
            $query = $query->orwhere($this->table.'.to_user_id', '=', $userInfo1->user_id);
        }

        if($userInfo2 && $userInfo1 == null) {
            $query = $query->where($this->table.'.user_id', '=', $userInfo2->user_id);
            $query = $query->orwhere($this->table.'.to_user_id', '=', $userInfo2->user_id);
        }

        if($userInfo1 && $userInfo2) {
            $where[] = [$this->table.'.user_id', '=', $userInfo1->user_id];
            $where[] = [$this->table.'.to_user_id', '=', $userInfo2->user_id];

            $where2[] = [$this->table.'.to_user_id', '=', $userInfo1->user_id];
            $where2[] = [$this->table.'.user_id', '=', $userInfo2->user_id];
            $query = $query->where($where);
            $query = $query->orwhere($where2);
        }

//        return $query->leftJoin(DB::raw('`kkaudio`.`zb_user` as b'), DB::raw('b.user_id'), $this->table.'.user_id')
//                ->leftJoin(DB::raw('`kkaudio`.`zb_user` as c'), DB::raw('c.user_id'), $this->table.'.to_user_id')
//                ->orderBy('dateTime', 'desc')
//                ->select($this->table.'.*', DB::raw('b.uuid as uuid1'), DB::raw('c.uuid as uuid2'))
//                ->paginate($limit, [$this->table . '.*'], 'page', $page);

        $dataObj = $query->orderBy('dateTime', 'desc')
                ->paginate($limit, [$this->table . '.*'], 'page', $page);
        $data = json_decode(json_encode($dataObj),true);
        $userids = array_column($data['data'],'user_id');
        $toUserids = array_column($data['data'],'to_user_id');
        $userids = array_merge($userids,$toUserids);

        $userData = DB::connection('api')->table('user')->select('uuid','user_id')->whereIn('user_id',$userids)->get();
        $userData = json_decode(json_encode($userData),true);
        $userData = array_column($userData,'uuid','user_id');
        foreach ($dataObj as $key => $v){
            $dataObj[$key]->uuid1 =$userData[$v->user_id]??'';
            $dataObj[$key]->uuid2 =$userData[$v->to_user_id]??'';
        }
        return $dataObj;


    }


    /**
     * 当日新用户聊天记录列表
     * @param Request $request
     */
    public function getNewChatLogList(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        $uuid1 = $request->get('uuid1', null);
        $uuid2 = $request->get('uuid2', null);

        $query = new ChatLog();

        $s = date('Y-m-d').' 00:00:00';
        $s = strtotime($s);

        $l = date('Y-m-d').' 23:59:59';
        $l = strtotime($l);

        $userInfo1 = DB::connection('api')->table('user')->select('user_id')->where('uuid', '=', $uuid1)->first();
        $userInfo2 = DB::connection('api')->table('user')->select('user_id')->where('uuid', '=', $uuid2)->first();

        if($userInfo1 && $userInfo2 == null) {
            $query = $query->where($this->table.'.user_id', '=', $userInfo1->user_id);
            $query = $query->orwhere($this->table.'.to_user_id', '=', $userInfo1->user_id);
        }

        if($userInfo2 && $userInfo1 == null) {
            $query = $query->where($this->table.'.user_id', '=', $userInfo2->user_id);
            $query = $query->orwhere($this->table.'.to_user_id', '=', $userInfo2->user_id);
        }

        if($userInfo1 && $userInfo2) {
            $where[] = [$this->table.'.user_id', '=', $userInfo1->user_id];
            $where[] = [$this->table.'.to_user_id', '=', $userInfo2->user_id];

            $where2[] = [$this->table.'.to_user_id', '=', $userInfo1->user_id];
            $where2[] = [$this->table.'.user_id', '=', $userInfo2->user_id];
            $query = $query->where($where);
            $query = $query->orwhere($where2);
        }

//        return $query->leftJoin(DB::raw('`kkaudio`.`zb_user` as b'), DB::raw('b.user_id'), $this->table.'.user_id')
//            ->leftJoin(DB::raw('`kkaudio`.`zb_user` as c'), DB::raw('c.user_id'), $this->table.'.to_user_id')
//            ->orderBy('dateTime', 'desc')
//            ->select($this->table.'.*', DB::raw('b.uuid as uuid1'), DB::raw('c.uuid as uuid2'))
//            ->where(DB::raw('b.create_timestamp'), '>=', $s)
//            ->where(DB::raw('b.create_timestamp'), '<=', $l)
//            ->paginate($limit, [$this->table . '.*'], 'page', $page);

        $dataObj = $query->leftJoin(DB::raw('`hypd`.`zb_user` as b'), DB::raw('b.user_id'), $this->table.'.user_id')
            ->orderBy('dateTime', 'desc')
            ->where(DB::raw('b.create_timestamp'), '>=', $s)
            ->where(DB::raw('b.create_timestamp'), '<=', $l)
            ->paginate($limit, [$this->table . '.*'], 'page', $page);
        $data = json_decode(json_encode($dataObj),true);
        $userids = array_column($data['data'],'user_id');
        $toUserids = array_column($data['data'],'to_user_id');
        $userids = array_merge($userids,$toUserids);

        $userData = DB::connection('api')->table('user')->select('uuid','user_id')->whereIn('user_id',$userids)->get();
        $userData = json_decode(json_encode($userData),true);
        $userData = array_column($userData,'uuid','user_id');
        foreach ($dataObj as $key => $v){
            $dataObj[$key]->uuid1 =$userData[$v->user_id]??'';
            $dataObj[$key]->uuid2 =$userData[$v->to_user_id]??'';
        }
        return $dataObj;

    }
}
